<template>
    <div>
        <!-- 面包屑导航区域 -->
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
            <el-breadcrumb-item>用户列表</el-breadcrumb-item>
        </el-breadcrumb>


        <!-- 卡片视图区域 -->
        <el-card>
            <!-- 搜索与添加区域 -->
            <div style="margin-top: 15px; margin-bottom:15px;">
                <el-row :gutter="20">
                    <el-col :span="7">
                        <el-input @clear="userList()" clearable placeholder="请输入内容" v-model="queryInfo.query">
                            <el-button slot="append" icon="el-icon-search" @click="userList()"></el-button>
                        </el-input>
                    </el-col>
                    <el-col  :span="4">
                        <el-button type="primary" @click="dialogVisible = true">添加用户</el-button>
                    </el-col>
                </el-row>
            </div>

            <!-- 用户列表区域 -->
            <el-table
                border
                :data="userList1"
                stripe
                style="width: 100%"
            >
                <el-table-column
                    label="#"
                    type="index"
                    width="180"
                >
                </el-table-column>


                <el-table-column
                    prop="username"
                    label="性名"
                    width="180"
                >
                </el-table-column>
                

                <el-table-column
                    prop="email"
                    label="邮箱"
                    width="180"
                >
                </el-table-column>


                <el-table-column
                    prop="mobile"
                    label="电话"
                >
                </el-table-column>


                <el-table-column
                    prop="role_name"
                    label="角色"
                >
                </el-table-column>


                <el-table-column
                    label="状态"
                >
                    <template v-slot="scope">
                        <el-switch
                            v-model="scope.row.mg_state"
                            active-color="#13ce66"
                            inactive-color="#ff4949"
                            @change="changeUserState(scope.row)"
                        >
                        </el-switch>
                    </template>
                </el-table-column>


                <el-table-column
                    label="操作"
                    width="180px"
                >
                    <template v-slot="scope">
                        <!-- 修改按钮 -->
                        <el-button size="mini" type="primary" icon="el-icon-edit" @click="changeEdit(scope.row.id)"></el-button>

                        <!-- 删除按钮 -->
                        <el-button size="mini" type="danger" icon="el-icon-delete" @click="deleteUser(scope.row.id)"></el-button>

                        <!-- 分配角色按钮 -->
                        <el-tooltip class="item" effect="dark" content="分配角色按钮" placement="top-start">
                            <el-button size="mini" type="warning" icon="el-icon-setting" @click="setRoleJue(scope.row)"></el-button>
                        </el-tooltip>
                         
                    </template>
                </el-table-column>

            </el-table>

            


            <!-- 分页区域 -->
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum"
                :page-sizes="[1, 2, 3, 4]"
                :page-size="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                
                :total="total">
            </el-pagination>
        </el-card>

        <!-- 弹出对话框 -->

            <el-dialog
                title="提示"
                :visible.sync="dialogVisible"
                width="50%"
                :close-on-click-modal="false"
                @close="stateReset"
            >

                <!-- 主体区域 -->
                <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="70px">

                    <!-- 用户名 -->
                    <el-form-item label="用户名" prop="username">
                        <el-input v-model="addForm.username"></el-input>
                    </el-form-item>

                    <!-- 密码 -->
                    <el-form-item label="密码" prop="password">
                        <el-input type="password" v-model="addForm.password"></el-input>
                    </el-form-item>

                    <!-- 邮箱 -->
                    <el-form-item label="邮箱" prop="email">
                        <el-input v-model="addForm.email"></el-input>
                    </el-form-item>

                    <!-- 电话 -->
                    <el-form-item label="电话" prop="phone">
                        <el-input v-model="addForm.mobile"></el-input>
                    </el-form-item>
                </el-form>

                <!-- 底部区域 -->
                <span slot="footer" class="dialog-footer">
                    <el-button @click="dialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="addUser">确 定</el-button>
                </span>
            </el-dialog>

        <!-- 弹出用户对话框 -->
        <el-dialog
        title="修改用户"
        :visible.sync="logVisible"
        width="50%"
        @close="editClosed()"
        :close-on-click-modal="false"
        >
            <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="70px">
                <el-form-item label="用户名">
                    <el-input v-model="editForm.username" disabled></el-input>
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="editForm.email"></el-input>
                </el-form-item>
                <el-form-item label="手机" prop="mobile">
                    <el-input v-model="editForm.mobile"></el-input>
                </el-form-item>
            </el-form>

        <span slot="footer" class="dialog-footer">
            <el-button @click="logVisible = false">取 消</el-button>
            <el-button type="primary" @click="editUserInfo()">确 定</el-button>
        </span>
        </el-dialog> 

        <!--分配角色对话框-->
        <el-dialog
        @close="Closedialog()"
            title="分配角色"
            :visible.sync="setRoleVisible"
            width="50%"
        >
            <div>
                <p>当前的用户：{{userInfo.username}}</p>
                <p>当前的角色：{{userInfo.role_name}}</p>
                <p>分配新角色：
                    <el-select  v-model="value" placeholder="请选择">
                        <el-option
                            v-for="item in this.roleList"
                            :key="item.id"
                            :label="item.roleName"
                            :value="item.id"
                        >
                        </el-option>
                    </el-select>
                </p>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="setRoleVisible = false">取 消</el-button>
                <el-button type="primary" @click="setUserFp()">确 定</el-button>
            </span>
        </el-dialog>

    </div>
</template>
<script>
export default {
    data() {
        return {
            queryInfo:{
                query:'',
                pagenum:1,
                pagesize:2
            },
            userList1:[],
            total:0,
            dialogVisible:false,
            logVisible:false,
            //添加表单信息
            addForm:{
                username:'',
                email:'',
                password:'',
                mobile:''
            },
            //添加表单规则对象
            addFormRules:{
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 3, max: 10, message: '用户名长度为3 ~ 10 个字符', trigger: 'blur' }
                ],
                email:[
                    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
                    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
                ],
                password:[
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 10, max: 16, message: '密码长度为10 ~ 16 个字符', trigger: 'blur' }
                ],
                mobile:[
                    { required: true, message: '请输入电话号码', trigger: 'blur' },
                    { min: 11, max: 12, message: '请输入正确的电话号码', trigger: 'blur' }
                ]
            },
            //查询到的用户信息对象
            editForm:{},
            editFormRules:{
                email:[
                    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
                    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
                ],
                mobile:[
                    { required: true, message: '请输入电话号码', trigger: 'blur' },
                    { min: 11, max: 12, message: '请输入正确的电话号码', trigger: 'blur' }
                ]
            },
            deletelogVisible:false,
            setRoleVisible:false,
            userInfo:{},
            roleList:[],
            value:''
        }
    },
    methods: {
        async userList(){
             const { data } = await this.axios.get('users',{params:this.queryInfo})

             if(data.meta.status !==200){
                 return this.$message.error('数据请求失败')
             }
             this.userList1 = data.data.users
             this.total = data.data.total
            //console.log(data)
        },
        handleSizeChange(val) {
            this.queryInfo.pagesize = val;
            this.userList()
        },
        handleCurrentChange(val) {

            this.queryInfo.pagenum = val;
            this.userList()
        },
        async changeUserState(userInfo){
            const { data } = await this.axios.put(`users/${userInfo.id}/state/${userInfo.mg_state}`)
            console.log(data)
            if(data.meta.status !== 200){
                userInfo.mg_state !=userInfo.mg_state
                this.$message.error('状态更新失败')
            }else{
                this.$message.success('状态更新成功')
            }
        },
        stateReset(){
            this.$refs.addFormRef.resetFields()
        },
        addUser(){
             this.$refs.addFormRef.validate( async vaild =>{
                 if(!vaild) return
                 this.$message.success('用户添加成功')
                 const { data } = await this.axios.post('users',this.addForm)
                 console.log(data)
                 this.dialogVisible=false

                 this.userList()
             })
        },

        //展示编辑用户的对话框
        async changeEdit(id){
            this.logVisible = true
            //console.log(id)
            const { data } = await this.axios.get("users/"+id)
            this.editForm=data.data
            //console.log(data)
        },
        editClosed(){
            this.$refs.editFormRef.resetFields()
        },
        editUserInfo(){
            this.logVisible = false
            this.$refs.editFormRef.validate(async vaild=>{
                //console.log(vaild)
                if(!vaild) return
                //发起修改用户信息的请求
                const { data } = await this.axios.put('users/'+this.editForm.id,{email:this.editForm.email,mobile:this.editForm.mobile}) 
                console.log(data)
                this.userList()

            })
        },
        
        async deleteUser(id){
            this.deletelogVisible = true

            const data1 = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).catch(err=>err)
            //console.log(data)
            if (data1 !== "confirm") {
                this.$message.info('已取消删除')
            }

            const { data } = await this.axios.delete('users/'+id)
            //console.log(data)
            
            if(data.meta.status == 200){
                this.$message.success('删除用户成功')
            }else{
                this.$message.success('删除用户失败')
            }
            this.userList()
        },
        //编辑角色按钮
        async setRoleJue(role){
            this.userInfo = role

            //获取所有角色列表
            const { data } = await this.axios.get('roles')
            //console.log(data)
            this.roleList = data.data
            this.setRoleVisible = true
        },
        //分配角色
        async setUserFp(){
            const { data } = await  this.axios.put(`users/${this.userInfo.id}/role`,{rid:this.value})
            console.log(data)
            this.userList()

            this.setRoleVisible = false
        },
        //关闭对话框后
        Closedialog(){
            this.value='',
            this.userInfo={}
        }
    },
    created() {
        this.userList()
    },
}
</script>
<style lang="less" scoped>
    .el-pagination{
        margin-top: 15px;
    }
</style>